﻿using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Socium.Domain {
	static class Serializer {
		public static T Load<T>(string path) {
			using (var file_stream = File.OpenRead(path))
			using (var database_stream = new DatabaseStream(file_stream, DocumentStreamMode.Read)) {
				var formatter = CreateFormatter();
				return (T) formatter.Deserialize(database_stream);
			}
		}

		public static void Save(string path, object obj) {
			using (var file_stream = File.OpenWrite(path))
			using (var database_stream = new DatabaseStream(file_stream, DocumentStreamMode.Write)) {
				var formatter = CreateFormatter();
				formatter.Serialize(database_stream, obj);
			}
		}

		static BinaryFormatter CreateFormatter() {
			return new BinaryFormatter();
		}
	}
}
